package com.rcd.viatornisl.android.model;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Base64;
import android.util.Log;

public class EstablishmentManager extends BaseManager {
	
	private final String ESTABLISHMENT = "establishments/";
	private final String ESTABLISHMENT_JSONLIST = "jsonlist/";
	private final String ESTABLISHMENT_JSONVIEW = "jsonview/";
	private final String ESTABLISHMENT_JSONQUERYPERINTEREST = "jsonqueryperinterest/";
	
	public EstablishmentManager() {
		// Setup the attributes
		this.setBaseUrl("");
	}
	
	public EstablishmentManager(String baseUrl) {
		// Setup the attributes
		this.setBaseUrl(baseUrl);
	}
	
	public Establishment getEstablishment(int id) {
		// Set the right url
		String url = String.format("%s%s%s%d", this.getBaseUrl(), ESTABLISHMENT, ESTABLISHMENT_JSONVIEW, id);
		
		try {
			// Get full content JSON from a url
			JSONObject json = new JSONObject(new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next());
			
			// Create an establishment object
			Establishment establishment = new Establishment();
			
			// Set the establishment properties
			establishment.setId(json.getJSONObject("Establishment").getInt("id"));
			establishment.setInterest(this.getInterest(json.getJSONObject("Establishment").getInt("interest_id")));
			establishment.setName(json.getJSONObject("Establishment").getString("name"));
			establishment.setLogo(Base64.decode(json.getJSONObject("Establishment").getString("logo"), Base64.DEFAULT));
			establishment.setSlogan(json.getJSONObject("Establishment").getString("slogan"));
			establishment.setDescription(json.getJSONObject("Establishment").getString("description"));
			establishment.setAddress(json.getJSONObject("Establishment").getString("address"));
			establishment.setCity(json.getJSONObject("Establishment").getString("city"));
			establishment.setState(json.getJSONObject("Establishment").getString("state"));
			establishment.setCountry(json.getJSONObject("Establishment").getString("country"));
			establishment.setWebsite(json.getJSONObject("Establishment").getString("website"));
			establishment.setPhone1(json.getJSONObject("Establishment").getString("phone1"));
			establishment.setPhone2(json.getJSONObject("Establishment").getString("phone2"));
			establishment.setCreated(establishment.loadDate(json.getJSONObject("Establishment").getString("created")));
			establishment.setModified(establishment.loadDate(json.getJSONObject("Establishment").getString("modified")));
			
			// Provide the return
			return establishment;
		} catch (Exception exception) {
			// Log the error
			Log.e("Establishment getEstablishment(int id)", exception.getLocalizedMessage());
						
			// On error
			exception.printStackTrace();
		}
		
		// Provide the return
		return null;
	}
	
	public List<Establishment> getEstablishments() {
		// Set the right url
		String url = String.format("%s%s%s", this.getBaseUrl(), ESTABLISHMENT, ESTABLISHMENT_JSONLIST);
				
		try {
			// Get full content JSON from a url
			JSONArray json = new JSONArray(new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next());

			// Create an establishment list
			List<Establishment> establishments = new ArrayList<Establishment>();
			
			// Fill the establishment
			for (int index = 0; index < json.length(); index++) {
				// Create a establishment object
				Establishment establishment = new Establishment();
				
				// Set the establishment properties
				establishment.setId(json.getJSONObject(index).getJSONObject("Establishment").getInt("id"));
				establishment.setInterest(this.getInterest(json.getJSONObject(index).getJSONObject("Establishment").getInt("interest_id")));
				establishment.setName(json.getJSONObject(index).getJSONObject("Establishment").getString("name"));				
				establishment.setLogo(Base64.decode(json.getJSONObject(index).getJSONObject("Establishment").getString("logo"), Base64.DEFAULT));
				establishment.setSlogan(json.getJSONObject(index).getJSONObject("Establishment").getString("slogan"));
				establishment.setDescription(json.getJSONObject(index).getJSONObject("Establishment").getString("description"));
				establishment.setAddress(json.getJSONObject(index).getJSONObject("Establishment").getString("address"));
				establishment.setCity(json.getJSONObject(index).getJSONObject("Establishment").getString("city"));
				establishment.setState(json.getJSONObject(index).getJSONObject("Establishment").getString("state"));
				establishment.setCountry(json.getJSONObject(index).getJSONObject("Establishment").getString("country"));
				establishment.setWebsite(json.getJSONObject(index).getJSONObject("Establishment").getString("website"));
				establishment.setPhone1(json.getJSONObject(index).getJSONObject("Establishment").getString("phone1"));
				establishment.setPhone2(json.getJSONObject(index).getJSONObject("Establishment").getString("phone2"));
				establishment.setCreated(establishment.loadDate(json.getJSONObject(index).getJSONObject("Establishment").getString("created")));
				establishment.setModified(establishment.loadDate(json.getJSONObject(index).getJSONObject("Establishment").getString("modified")));
				
				// Add the establishment
				establishments.add(establishment);
			}
			
			// Provide the return
			return establishments;
		} catch (Exception exception) {
			// Log the error
			Log.e("List<Establishment> getEstablishments()", exception.getLocalizedMessage());
						
			// On error
			exception.printStackTrace();
		}
		
		// Provide the return
		return null;
	}

	public List<Establishment> getEstablishments(int interest_id) {
		// Set the right url
		String url = String.format("%s%s%s%d", this.getBaseUrl(), ESTABLISHMENT, ESTABLISHMENT_JSONQUERYPERINTEREST, interest_id);
				
		try {
			// Get full content JSON from a url
			JSONArray json = new JSONArray(new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next());

			// Create an establishment list
			List<Establishment> establishments = new ArrayList<Establishment>();
			
			// Fill the establishment
			for (int index = 0; index < json.length(); index++) {
				// Create a establishment object
				Establishment establishment = new Establishment();
				
				// Set the establishment properties
				establishment.setId(json.getJSONObject(index).getJSONObject("Establishment").getInt("id"));
				establishment.setInterest(this.getInterest(json.getJSONObject(index).getJSONObject("Establishment").getInt("interest_id")));
				establishment.setName(json.getJSONObject(index).getJSONObject("Establishment").getString("name"));
				establishment.setLogo(Base64.decode(json.getJSONObject(index).getJSONObject("Establishment").getString("logo"), Base64.DEFAULT));
				establishment.setSlogan(json.getJSONObject(index).getJSONObject("Establishment").getString("slogan"));
				establishment.setDescription(json.getJSONObject(index).getJSONObject("Establishment").getString("description"));
				establishment.setAddress(json.getJSONObject(index).getJSONObject("Establishment").getString("address"));
				establishment.setCity(json.getJSONObject(index).getJSONObject("Establishment").getString("city"));
				establishment.setState(json.getJSONObject(index).getJSONObject("Establishment").getString("state"));
				establishment.setCountry(json.getJSONObject(index).getJSONObject("Establishment").getString("country"));
				establishment.setWebsite(json.getJSONObject(index).getJSONObject("Establishment").getString("website"));
				establishment.setPhone1(json.getJSONObject(index).getJSONObject("Establishment").getString("phone1"));
				establishment.setPhone2(json.getJSONObject(index).getJSONObject("Establishment").getString("phone2"));
				establishment.setCreated(establishment.loadDate(json.getJSONObject(index).getJSONObject("Establishment").getString("created")));
				establishment.setModified(establishment.loadDate(json.getJSONObject(index).getJSONObject("Establishment").getString("modified")));
				
				// Add the establishment
				establishments.add(establishment);
			}
			
			// Provide the return
			return establishments;
		} catch (Exception exception) {
			// Log the error
			Log.e("List<Establishment> getEstablishments(int interest_id)", exception.getLocalizedMessage());
			
			// On error
			exception.printStackTrace();
		}
		
		// Provide the return
		return null;
	}
	
	private Interest getInterest(int interest_id) {
		try {
			// Create a manager object
			InterestManager interestManager = new InterestManager(this.getBaseUrl());
			
			// Provide the return
			return interestManager.getInterest(interest_id);
		} catch (Exception exception) {
			// Log the error
			Log.e("Interest getInterest(int interest_id)", exception.getLocalizedMessage());
						
			// On error
			exception.printStackTrace();
		}
		
		// Provide the return
		return null;
	}

}
